package com.tongjie.hotelbooking.remote;

import com.alibaba.fastjson.JSON;
import com.tongjie.hotelbooking.utils.HttpUtils;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class WechatApi {

    public Jscode2sessionResponse jscode2session(String jsCode) {
       String url = "https://api.weixin.qq.com/sns/jscode2session";

        Map<String, String> params = new HashMap<>();
        params.put("appid", "wx18c4a52b2b5c0be7");
        params.put("secret", "e7f66418fe14c436d43c8f19b1cc2df6");
        params.put("js_code", jsCode);
        params.put("grant_typ", "authorization_code");
        String returnStr = HttpUtils.httpsGet(url, params);
        return returnStr == null ? null : JSON.parseObject(returnStr, Jscode2sessionResponse.class);
    }

    @Data
    public static class Jscode2sessionResponse {
        private String session_key;
        private String openid;
    }

}
